package com.itheima.web.listener;

import com.itheima.autoconfig.green.AliyunGreenTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MovementStateListener {

    @DubboReference
    MovementService movementService;

    @Autowired
    AliyunGreenTemplate aliyunGreenTemplate;

    @RabbitListener(queuesToDeclare = @Queue("tanhua.movement.state"))
    public void checkMovement(ObjectId id){
        //1.查询movement对象
        Movement movement = movementService.findById(id);

        //2.检查文本
        Boolean textResult = aliyunGreenTemplate.checkText(movement.getTextContent());

        //3.检查图片
        Boolean imgResult = aliyunGreenTemplate.checkImage(movement.getMedias());

        if (textResult && imgResult) {
            //4.若都没有问题,设置动态的状态为1
            movement.setState(1);
        }else {
            //5.若其中一个有问题,设置动态的状态为2
            movement.setState(2);
        }

        //6.更新到数据库中
        movementService.update(movement);
    }

}
